// pages/setaddress/setaddress.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        uname: null,
        tel: null,
        addre: null,
        index: null,
        pid: null,
        addres:null,

    },
    del(options) {
        console.log(options);
        let id = parseInt(options.currentTarget.dataset.id);
        let fromLists = wx.getStorageSync('fromLists');
        if (id) {
            let index = fromLists.findIndex(item => item.id == id);
            fromLists.splice(index, 1);
            wx.setStorageSync('fromLists', fromLists);
        }
    },
    // 表单提交
    formSubmit(e) {
        // console.log(e.detail.value.tel);
        var warn = "";
        var that = this;
        var flag = false;
        if (e.detail.value.uname == "") {
            warn = "请填写您的姓名！";
        } else if (e.detail.value.tel == "") {
            warn = "请填写您的手机号！";
        }
        else if (!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.tel))) {
            warn = "手机号格式不正确";
        } else if (e.detail.value.addre == '') {
            warn = "请选择您的所在区域";
        }
        else {
            flag = true;
            // wx.redirectTo关闭当前页面,跳转到另外一个页面
            wx.redirectTo({
                url: '/pages/address/address',
            })
            // let index = fromLists.findIndex(item => item.id == this.data.pid);
            console.log("this.data.index", this.data.index);
            // console.log("index");
            let fromLists = wx.getStorageSync('fromLists');
            let index = this.data.index;
            if (index || index == 0) {
                fromLists[index].name = e.detail.value.uname;
                fromLists[index].tel = e.detail.value.tel;
                fromLists[index].addre = e.detail.value.addre;
                fromLists[index].id = parseInt(this.data.pid);
                wx.setStorageSync('fromLists', fromLists)
                // console.log("修改的fromLists",fromLists);

            } else {
                let lis = {
                    id: new Date().getTime(),
                    name: e.detail.value.uname,
                    tel: e.detail.value.tel,
                    addre: e.detail.value.addre,
                    checked: false,
                };
                fromLists.push(lis);
                wx.setStorageSync('fromLists', fromLists)
            }


        }
        if (flag == false) {
            wx.showModal({
                title: '提示',
                content: warn
            })
        }
        // console.log('form发生了submit事件，携带数据为：', e.detail.value)
    },
    chooseAddtr(){
       let  _this = this;
        wx.chooseLocation({
          latitude: 0,
          success (res) {
                  console.log('res',res);
                _this.setData({
                    addre: res.address
                })
                }
        })
        
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let fromLists = wx.getStorageSync('fromLists');
        if (fromLists) {
            // fromLists.push(this.data.fromList)
            wx.setStorageSync('fromLists', fromLists)
            // console.log('fromLists', fromLists);
        } else {
            wx.setStorageSync('fromLists', [])
        }
        // 修改地址信息
        console.log("修改地址信息", options);
        let index = fromLists.findIndex(item => item.id == options.id);
        console.log(index);
        if (index != -1) {
            this.setData({
                uname: fromLists[index].name,
                tel: fromLists[index].tel,
                addre: fromLists[index].addre,
                index: index,
                pid: options.id
            })
        }

        console.log("pid", this.data.pid);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})